package component.renderers
{

	import com.adobe.cairngorm.control.CairngormEventDispatcher;

	import define.Localization;

	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;

	import mvc.command.client.IArmoryTipClient;
	import mvc.control.ArmoryEvent;

	import mx.containers.HBox;
	import mx.controls.AdvancedDataGrid;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
	import mx.controls.listClasses.BaseListData;
	import mx.controls.listClasses.IDropInListItemRenderer;
	import mx.events.ToolTipEvent;

	import orm.fantasy.TblItemVO;

	import vo.ArmoryBean;

	public class RendererIconLabel extends HBox implements IDropInListItemRenderer, IArmoryTipClient
	{
		private var _icon:Image;
		private var _label:Label;

		private static var _l:Localization = Localization.instance;

		public function RendererIconLabel()
		{
			super();

			super.horizontalScrollPolicy = "false";
			super.verticalScrollPolicy = "false";
			super.setStyle("verticalAlign", "middle");
						
			this.addEventListener(ToolTipEvent.TOOL_TIP_CREATE, design_toolTipCreateHandler);

			_icon = new Image();
			_icon.addEventListener(Event.INIT, icon_initListener);
			this.addChild(_icon);

			_label = new Label();
			this.addChild(_label);

			_label.addEventListener(MouseEvent.CLICK, label_clickHandler);
		}

		private function label_clickHandler(event:MouseEvent):void
		{
			Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, _label.text);
		}
		private var _iconDir:String;

		public function set iconDir(data:String):void
		{
			_iconDir = data;
		}

		private var _iconDataFields:Array;

		public function set iconDataFields(data:String):void
		{
			_iconDataFields = data.split(".");
		}

		private var _labelDataFields:Array;

		public function set labelDataFields(data:String):void
		{
			_labelDataFields = data.split(".");
		}

		private var _itemPropertyBean:ArmoryBean;

		private var _listData:AdvancedDataGridListData;

		private var _dg:AdvancedDataGrid;
		private var _col:AdvancedDataGridColumn;

		public function set listData(value:BaseListData):void
		{
			// 拖拽时
			if((value as AdvancedDataGridListData).item == null)
				return;

			if(_dg == null)
			{
				_dg = value.owner as AdvancedDataGrid;
				_col = _dg.columns[value.columnIndex] as AdvancedDataGridColumn;
			}

			_listData = value as AdvancedDataGridListData;

			_iconWidth = (_listData.owner as AdvancedDataGrid).rowHeight;
			_iconHeight = (_listData.owner as AdvancedDataGrid).rowHeight;

			_itemPropertyBean = _listData.item as ArmoryBean;

			loadIcon();
			initLabel();
			// 第一列，名称
			if(_col.headerText == _l.s("item_name"))
			{
				this.toolTip = "item_name";
				_label.setStyle("color", 0xa335ee);
				_label.setStyle("textDecoration", "underline");
				if(_itemPropertyBean.itemPropertyVo.info_item_id == "46017")
					_label.setStyle("color", 0xff8000);
			}
			else if(_col.headerText == _l.s("acquisition"))
			{
//				if(_label.text.indexOf("Tier 7") != -1)
//					this.toolTip = _l.s("description_tier7");
//				else if(_label.text.indexOf("Tier 8") != -1 && _label.text.indexOf("10") != -1)
//					this.toolTip = _l.s("description_tier8_10");
//				else if(_label.text.indexOf("Tier 8") != -1 && _label.text.indexOf("25") != -1)
//					this.toolTip = _l.s("description_tier8_25");
//				else if(_label.text.indexOf("Tier 9") != -1 && _label.text.indexOf("232") != -1)
//					this.toolTip = _l.s("description_tier9_232");
//				else if(_label.text.indexOf("Tier 9") != -1 && _label.text.indexOf("245") != -1)
//					this.toolTip = _l.s("description_tier9_245");
//				else if(_label.text.indexOf("Tier 9") != -1 && _label.text.indexOf("258") != -1)
//					this.toolTip = _l.s("description_tier9_258");
			}
		}

		private function loadIcon():void
		{
			var temp:Object = _itemPropertyBean;
			for each(var field:String in _iconDataFields)
			{
				temp = temp[field];
			}

			if(temp != null && temp != "")
			{
				var suffix:String = ".jpg";
				if(_iconDir == "side")
					suffix = ".png";

				_icon.load(File.applicationDirectory.resolvePath(
					"app:/icon/" + _iconDir + "/" + temp + suffix).nativePath);
				_icon.includeInLayout = true;
			}
			else
			{
				_icon.unloadAndStop();
				_icon.includeInLayout = false;
			}
		}

		private function initLabel():void
		{
			var temp:Object = _itemPropertyBean;
			for each(var field:String in _labelDataFields)
			{
				temp = temp[field];
			}

			_label.text = temp as String;
		}

		private var _iconWidth:int;
		private var _iconHeight:int;

		private function icon_initListener(event:Event):void
		{
			var image:Image = event.target as Image;
			var scaleX:Number = (_iconWidth - 4) / image.contentWidth;
			var scaleY:Number = (_iconHeight - 4) / image.contentHeight;

			if(scaleY < 1 || scaleX < 1)
			{
				var scale:Number = Math.min(scaleY, scaleX);

				image.height = image.contentHeight * scale;
				image.width = image.contentWidth * scale;
			}
		}

		public function get listData():BaseListData
		{
			return _listData;
		}

		protected function design_toolTipCreateHandler(event:ToolTipEvent):void
		{
			// 第一列，名称
			if(_col.headerText == _l.s("item_name"))
			{
				var mvcEvent:ArmoryEvent = new ArmoryEvent(ArmoryEvent.SHOW_ARMORY_TIP);
				mvcEvent.data = {client: this, toolTipEvent: event};
				CairngormEventDispatcher.getInstance().dispatchEvent(mvcEvent);
			}
		}

		public function get tipTblItemVO():TblItemVO
		{
			return _itemPropertyBean.itemVo;
		}
	}
}
